package net.huashitong.Project.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.dao.BuildProjectsDao;
import net.huashitong.Project.entity.BuildProjects;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.BuildProjectsFilter;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.responseModel.BuildProjectsModel;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BuildProjectsService extends StringPKService<BuildProjects> {

    @Autowired
    private BuildProjectsDao dao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private NewProjectsService newProjectsService;

    @Override
    protected BaseDao<BuildProjects, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(BuildProjects obj) {
        obj.setSysId(tableService.updateMaxSysId("t_bnp_build_project", null));
    }

    public Map<String, Object> queryBuildProjectsList(BnpUser user, BuildProjectsFilter filter) {
        Map<String, Object> map = new HashMap<>();
        List<BuildProjectsModel> result = new ArrayList<>();
        String contactPhone = user.getPhone();
        NewProjectsFilter projectsFilter = new NewProjectsFilter();
        projectsFilter.setContactPhone(contactPhone);
        projectsFilter.setStatus("4");
        projectsFilter.setPaging(filter.getPaging());
        List<NewProjects> projects = newProjectsService.getProjectInfoPage(projectsFilter);
        for (NewProjects project : projects) {
            BuildProjectsModel buildProject = new BuildProjectsModel();
            BuildProjects buildProjects = queryNewestData(project.getSysId());
            BeanUtils.copyProperties(project, buildProject);
            if (ObjectUtils.isNotBlank(buildProjects)) {
                buildProjects.setSysId(buildProject.getSysId());
                BeanUtils.copyProperties(buildProjects, buildProject);
            }
            result.add(buildProject);
        }
        if (projects.size() == 0) {
            map.put("pageCount", 0);
        } else {
            map.put("pageCount", projectsFilter.getPaging().getRecords());
        }
        map.put("list", result);
        return map;
    }

    public void addBuildRecord(BuildProjectsFilter filter, BnpUser bnpUser) {
        BuildProjectsFilter f = new BuildProjectsFilter();
        f.setDataTime(filter.getDataTime());
        f.setProjectId(filter.getProjectId());
        List<BuildProjects> list = dao.queryBuildProjectsByFilter(f);
        if (list.size() > 0) {
            //如果有该时间的数据则更新，否则新增
            BuildProjects buildProjects = list.get(0);
            filter.setSysId(buildProjects.getSysId());
            BeanUtils.copyProperties(filter, buildProjects);
            this.onPreUpdate(buildProjects);
            this.beforeUpdate(buildProjects);
            buildProjects.setUpdUser(bnpUser.getSysId());
            this.updateObject(buildProjects);
            this.afterUpdate(buildProjects);
        }else{
            BuildProjects buildProjects = new BuildProjects();
            BeanUtils.copyProperties(filter, buildProjects);
            this.onPrePersist(buildProjects);
            this.beforeSave(buildProjects);
            buildProjects.setCreateUser(bnpUser.getSysId());
            this.saveObject(buildProjects);
            this.afterSave(buildProjects);
        }
    }

    public List<BuildProjects> queryBuildProjectsByFilter(BuildProjectsFilter filter){
        return  dao.queryBuildProjectsByFilter(filter);
    }




    public BuildProjects queryNewestData(String projectId) {
        return dao.queryNewestData(projectId);
    }
}
